In [1]:
import pandas as pd
import numpy as np
import requests 
from geopy.geocoders import Nominatim   
import folium                           
from folium import plugins              
import openrouteservice                 
from openrouteservice import convert
import geopy.distance                   
%matplotlib inline
In [15]:
ride_data = pd.read_csv('final_data_volume.csv')
In [16]:
ride_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4528 entries, 0 to 4527
Data columns (total 27 columns):
sno              4528 non-null int64
id               4528 non-null int64
maplat           4528 non-null float64
maplong          4528 non-null float64
stdate           4528 non-null object
stime            4528 non-null object
enddate          4528 non-null object
etime            4528 non-null object
pcount           4528 non-null int64
distance         4528 non-null float64
plo              4528 non-null float64
pla              4528 non-null float64
dlo              4528 non-null float64
dla              4528 non-null float64
delay            4528 non-null float64
gender           4528 non-null int64
gp               4528 non-null int64
occupation       4528 non-null object
op               4528 non-null object
smoker           4528 non-null int64
sp               4528 non-null int64
maritalstatus    4528 non-null int64
mp               4528 non-null int64
lang             4528 non-null object
lp               4528 non-null object
duration         4528 non-null int64
speed            4528 non-null object
dtypes: float64(8), int64(10), object(9)
memory usage: 955.2+ KB
In [17]:
pd.set_option('display.max_columns', 500)
In [18]:
ride_data.count()
Out[18]:
sno              4528
id               4528
maplat           4528
maplong          4528
stdate           4528
stime            4528
enddate          4528
etime            4528
pcount           4528
distance         4528
plo              4528
pla              4528
dlo              4528
dla              4528
delay            4528
gender           4528
gp               4528
occupation       4528
op               4528
smoker           4528
sp               4528
maritalstatus    4528
mp               4528
lang             4528
lp               4528
duration         4528
speed            4528
dtype: int64
In [19]:
def generate_map(map_location, map_style, start_lat_col, start_long_col, start_color, end_lat_col, end_long_col, end_color):
      
    folium_map = folium.Map(location=map_location,
                            zoom_start=11,
                            tiles=map_style)
    
    for index, row in ride_data.iterrows():  
        
        folium.CircleMarker(location=(row[start_lat_col],
                                      row[start_long_col]),
                            color=start_color,
                            radius=5,
                            weight=1,
                            fill=True).add_to(folium_map)
        
        folium.CircleMarker(location=(row[end_lat_col],
                                      row[end_long_col]),
                            color=end_color,
                            radius=5,
                            weight=1,
                            fill=True).add_to(folium_map)
        
    return folium_map
In [20]:
ride_data = ride_data[np.isfinite(ride_data['pla'])]
ride_data = ride_data[np.isfinite(ride_data['plo'])]

ride_data = ride_data[np.isfinite(ride_data['dla'])]
ride_data = ride_data[np.isfinite(ride_data['dlo'])]

ride_data = ride_data[np.isfinite(ride_data['maplat'])]
ride_data = ride_data[np.isfinite(ride_data['maplong'])]
In [21]:
generate_map([40.7128, -74.008],"cartodbpositron","pla", "plo",'#0A8A9F',"maplat", "maplong",'#f68e16')
Out[21]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [9]:
def get_paths(df):
    
    path_list = []
    
    for index, row in df.iterrows():
        
            coords = [(row['plo'],row['pla']),(row['maplong'],row['maplat'])]
            print(coords)
            
            client = openrouteservice.Client(key='5b3ce3597851110001cf6248c196e442e9f148a9a1863fa5c168e7bc') 
            geometry = client.directions(coords)['routes'][0]['geometry']
            decoded = convert.decode_polyline(geometry)
            reverse = [(y,x) for x,y in decoded['coordinates']]        
            path_list.append(reverse)
            print(index)
    
        
    return path_list
    
In [10]:
routes = get_paths(ride_data)
[(-73.7818, 40.6449), (-73.9379, 40.7262)]
0
[(-73.7819, 40.6448), (-73.9793, 40.6616)]
1
[(-73.7898, 40.6469), (-73.9821, 40.773)]
2
[(-73.7887, 40.6473), (-73.9129, 40.7651)]
3
[(-73.7811, 40.645), (-73.9648, 40.683)]
4
[(-73.7899, 40.6466), (-73.9556, 40.6405)]
5
[(-73.7899, 40.647), (-73.7686, 40.6998)]
6
[(-73.7815, 40.6448), (-73.8247, 40.6815)]
7
[(-73.7813, 40.645), (-73.9587, 40.8025)]
8
[(-73.79, 40.6471), (-73.9281, 40.76)]
9
[(-73.7898, 40.647), (-73.9352, 40.7695)]
10
[(-73.7898, 40.6471), (-73.8268, 40.6965)]
11
[(-73.7819, 40.6448), (-74.1648, 40.5874)]
12
[(-73.7819, 40.6448), (-73.874, 40.7739)]
13
[(-73.7833, 40.6487), (-73.76899999999999, 40.681999999999995)]
14
[(-73.7903, 40.6467), (-73.9676, 40.6319)]
15
[(-73.7818, 40.6447), (-73.9403, 40.7235)]
16
[(-73.7821, 40.6449), (-73.9488, 40.8134)]
17
[(-73.7897, 40.6471), (-73.7627, 40.6875)]
18
[(-73.7819, 40.6445), (-73.9502, 40.6289)]
19
[(-73.7899, 40.6468), (-74.0164, 40.7049)]
20
[(-73.7885, 40.6419), (-73.7963, 40.6683)]
21
[(-73.7821, 40.6448), (-73.9034, 40.6334)]
22
[(-73.7822, 40.6446), (-74.0103, 40.7235)]
23
[(-73.7823, 40.6448), (-73.9579, 40.6708)]
24
[(-73.7902, 40.6434), (-74.0083, 40.7077)]
25
[(-73.782, 40.6447), (-73.7468, 40.7142)]
26
[(-73.7821, 40.6447), (-73.9809, 40.7893)]
27
[(-73.7821, 40.6445), (-73.9877, 40.7326)]
28
[(-73.7886, 40.6419), (-73.8599, 40.6615)]
29
[(-73.7819, 40.6448), (-74.0344, 40.7298)]
30
[(-73.7831, 40.6468), (-73.9599, 40.7002)]
31
[(-73.7826, 40.6445), (-73.8273, 40.7007)]
32
[(-73.7823, 40.6444), (-73.7017, 40.6928)]
33
[(-73.7899, 40.6434), (-73.9337, 40.8568)]
34
[(-73.7818, 40.6448), (-73.956, 40.7445)]
35
[(-73.7814, 40.6449), (-73.8591, 40.7195)]
36
[(-73.7837, 40.6487), (-73.9568, 40.687)]
37
[(-73.7821, 40.6446), (-73.934, 40.668)]
38
[(-73.7816, 40.6451), (-73.9868, 40.7511)]
39
[(-73.7823, 40.6445), (-73.7994, 40.7054)]
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 1st time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 2nd time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 3rd time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 4th time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 5th time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 6th time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 7th time.
  stacklevel=1)
C:\ProgramData\Anaconda3\lib\site-packages\openrouteservice\client.py:214: UserWarning: Rate limit exceeded. Retrying for the 8th time.
  stacklevel=1)
40
[(-73.7819, 40.6447), (-73.7778, 40.7433)]
41
[(-73.7827, 40.6444), (-73.9336, 40.6725)]
42
[(-73.7901, 40.6433), (-73.9727, 40.7959)]
43
[(-73.7842, 40.6486), (-73.9606, 40.6595)]
44
[(-73.79, 40.6466), (-73.9109, 40.6591)]
45
[(-73.7797, 40.647), (-74.028, 40.6236)]
46
[(-73.7818, 40.6448), (-73.8252, 40.7577)]
47
[(-73.7819, 40.6447), (-73.8512, 40.5746)]
48
[(-73.79, 40.6466), (-73.9977, 40.7208)]
49
[(-73.7802, 40.6455), (-73.9537, 40.7577)]
50
[(-73.7819, 40.6448), (-73.9403, 40.751)]
51
[(-73.7901, 40.647), (-73.9253, 40.6683)]
52
[(-73.79, 40.6469), (-73.827, 40.6944)]
53
[(-73.79, 40.6468), (-73.9168, 40.6387)]
54
[(-73.7899, 40.6469), (-73.9449, 40.6697)]
55
[(-73.7896, 40.6468), (-73.9567, 40.7866)]
56
[(-73.7819, 40.6447), (-73.8375, 40.6913)]
57
[(-73.7897, 40.6467), (-73.7384, 40.6184)]
58
[(-73.791, 40.6438), (-73.8894, 40.6655)]
59
[(-73.7898, 40.6477), (-73.9923, 40.7345)]
60
[(-73.7789, 40.6467), (-73.9971, 40.7472)]
61
[(-73.7869, 40.6466), (-73.7546, 40.662)]
62
[(-73.7819, 40.6447), (-73.8025, 40.6909)]
63
[(-73.782, 40.6443), (-73.8232, 40.7582)]
64
[(-73.7899, 40.6469), (-73.9033, 40.8653)]
65
[(-73.7899, 40.6469), (-73.888, 40.6673)]
66
[(-73.7819, 40.6446), (-73.9497, 40.7962)]
67
[(-73.782, 40.6446), (-73.9709, 40.7985)]
68
[(-73.7828, 40.6444), (-73.753, 40.7069)]
69
[(-73.7902, 40.6463), (-73.9818, 40.7284)]
70
[(-73.7818, 40.6447), (-73.9256, 40.6686)]
71
[(-73.782, 40.6446), (-73.9878, 40.6288)]
72
[(-73.79, 40.6466), (-73.8913, 40.6765)]
73
[(-73.7818, 40.6447), (-73.8205, 40.6787)]
74
[(-73.7902, 40.6436), (-73.9751, 40.7585)]
75
[(-73.7836, 40.6486), (-73.9938, 40.6959)]
76
[(-73.79, 40.6462), (-73.9802, 40.7223)]
77
[(-73.7787, 40.6467), (-73.7984, 40.9454)]
78
[(-73.7896, 40.6467), (-73.9146, 40.6766)]
79
[(-73.7819, 40.6449), (-73.7723, 40.7811)]
80
[(-73.7897, 40.6468), (-73.9992, 40.7392)]
81
[(-73.7898, 40.6467), (-73.9689, 40.786)]
82
[(-73.7898, 40.6465), (-73.9466, 40.7807)]
83
[(-73.7909, 40.6465), (-73.9791, 40.7669)]
84
[(-73.7818, 40.6446), (-73.9881, 40.7698)]
85
[(-73.7902, 40.6467), (-73.7963, 40.6683)]
86
[(-73.782, 40.6447), (-73.9524, 40.7865)]
87
[(-73.7902, 40.6464), (-74.0112, 40.7063)]
88
[(-73.7903, 40.6464), (-74.008, 40.7311)]
89
[(-73.7818, 40.6447), (-73.9474, 40.7249)]
90
[(-73.7898, 40.6465), (-73.7717, 40.681)]
91
[(-73.7812, 40.645), (-73.8066, 40.6654)]
92
[(-73.7824, 40.6445), (-73.7405, 40.6742)]
93
[(-73.7822, 40.6447), (-73.9354, 40.6238)]
94
[(-73.782, 40.6444), (-73.92399999999999, 40.6191)]
95
[(-73.7895, 40.647), (-73.9527, 40.6525)]
96
[(-73.7889, 40.6472), (-73.733, 40.6522)]
97
[(-73.7822, 40.6446), (-73.9202, 40.6837)]
98
[(-73.7901, 40.6468), (-73.9373, 40.6624)]
99
[(-73.7824, 40.6445), (-74.0214, 40.7638)]
100
[(-73.7901, 40.6434), (-73.9538, 40.6629)]
101
[(-73.7824, 40.6445), (-73.8728, 40.668)]
102
[(-73.7897, 40.6437), (-73.9639, 40.757)]
103
[(-73.7819, 40.6446), (-73.7564, 40.6724)]
104
[(-73.7822, 40.6446), (-73.9537, 40.7577)]
105
[(-73.7821, 40.6445), (-73.9935, 40.7571)]
106
[(-73.7821, 40.6447), (-73.9117, 40.6695)]
107
[(-73.7818, 40.6448), (-73.9938, 40.7466)]
108
[(-73.7811, 40.645), (-73.7599, 40.6709)]
109
[(-73.7819, 40.6447), (-73.9807, 40.7338)]
110
[(-73.7822, 40.6446), (-73.9628, 40.6605)]
111
[(-73.7819, 40.6447), (-73.9932, 40.731)]
112
[(-73.7811, 40.6475), (-73.9823, 40.7516)]
113
[(-73.782, 40.6447), (-73.8935, 40.8429)]
114
[(-73.7822, 40.6446), (-73.9886, 40.7691)]
115
[(-73.7821, 40.6446), (-73.8845, 40.872)]
116
[(-73.7822, 40.6447), (-73.9606, 40.6595)]
117
[(-73.7819, 40.6447), (-73.7601, 40.6675)]
118
[(-73.782, 40.6446), (-73.9743, 40.7565)]
119
[(-73.7818, 40.6448), (-73.967, 40.7935)]
120
[(-73.7821, 40.6446), (-73.9752, 40.7445)]
121
[(-73.7818, 40.6447), (-73.9613, 40.7145)]
122
[(-73.7821, 40.6446), (-74.0152, 40.7098)]
123
[(-73.7821, 40.6445), (-73.9936, 40.736)]
124
[(-73.7811, 40.645), (-73.9948, 40.7278)]
125
[(-73.7818, 40.6448), (-74.0061, 40.5794)]
126
[(-73.7819, 40.6447), (-73.9709, 40.7484)]
127
[(-73.7818, 40.6447), (-73.8358, 40.6783)]
128
[(-73.7821, 40.6446), (-73.9977, 40.6787)]
129
[(-73.7821, 40.6447), (-73.7624, 40.6843)]
130
[(-73.7818, 40.6447), (-73.7465, 40.6968)]
131
[(-73.7811, 40.645), (-73.854, 40.6866)]
132
[(-73.782, 40.6447), (-73.9444, 40.8347)]
133
[(-73.7822, 40.6446), (-73.8168, 40.7135)]
134
[(-73.782, 40.6447), (-73.808, 40.6843)]
135
[(-73.782, 40.6447), (-73.9492, 40.6954)]
136
[(-73.7821, 40.6445), (-73.9376, 40.8127)]
137
[(-73.7816, 40.6448), (-73.7963, 40.6683)]
138
[(-73.7821, 40.6448), (-73.9149, 40.6633)]
139
[(-73.7819, 40.6448), (-73.9532, 40.7885)]
140
[(-73.7812, 40.645), (-73.953, 40.7917)]
141
[(-73.7787, 40.6467), (-73.9564, 40.6559)]
142
[(-73.7824, 40.6444), (-73.9133, 40.6936)]
143
[(-73.782, 40.6446), (-73.985, 40.7536)]
144
[(-73.7807, 40.6451), (-73.9254, 40.8251)]
145
[(-73.7818, 40.6446), (-73.9745, 40.6807)]
146
[(-73.7823, 40.6445), (-73.7057, 40.7128)]
147
[(-73.7818, 40.6448), (-73.9864, 40.7517)]
148
[(-73.7787, 40.6467), (-73.9596, 40.8012)]
149
[(-73.7821, 40.6447), (-73.7252, 40.7357)]
150
[(-73.782, 40.6448), (-73.9687, 40.6739)]
151
[(-73.7822, 40.6447), (-73.7963, 40.6683)]
152
[(-73.7821, 40.6446), (-73.9536, 40.6911)]
153
[(-73.7822, 40.6446), (-73.911, 40.7425)]
154
[(-73.782, 40.6446), (-73.9558, 40.693000000000005)]
155
[(-73.7818, 40.6449), (-73.9124, 40.8794)]
156
[(-73.7818, 40.6447), (-73.9429, 40.7775)]
157
[(-73.7818, 40.6448), (-73.9825, 40.7426)]
158
[(-73.782, 40.6447), (-73.9153, 40.7662)]
159
[(-73.7822, 40.6448), (-73.707, 40.6373)]
160
[(-73.7817, 40.6448), (-73.9421, 40.6703)]
161
[(-73.782, 40.6444), (-73.8341, 40.7178)]
162
[(-73.7823, 40.6451), (-74.002, 40.7351)]
163
[(-73.7823, 40.6447), (-73.9812, 40.7793)]
164
[(-73.7821, 40.6443), (-73.9517, 40.7736)]
165
[(-73.7821, 40.6449), (-73.9656, 40.5903)]
166
[(-73.7822, 40.6453), (-73.8182, 40.7201)]
167
[(-73.7822, 40.6447), (-73.9728, 40.7559)]
168
[(-73.7819, 40.6447), (-73.9524, 40.8239)]
169
[(-73.782, 40.6447), (-73.9791, 40.7404)]
170
[(-73.782, 40.6448), (-73.7413, 40.6116)]
171
[(-73.7813, 40.645), (-73.959, 40.7835)]
172
[(-73.7822, 40.6447), (-73.9063, 40.6316)]
173
[(-73.7822, 40.6445), (-73.8057, 40.6857)]
174
[(-73.7823, 40.6447), (-74.12899999999999, 40.8742)]
175
[(-73.7803, 40.6452), (-73.9453, 40.666)]
176
[(-73.7821, 40.6448), (-73.9959, 40.6696)]
177
[(-73.782, 40.6449), (-74.1724, 40.8491)]
178
[(-73.7865, 40.6453), (-73.98100000000001, 40.7478)]
179
[(-73.7822, 40.6451), (-74.0055, 40.7388)]
180
[(-73.782, 40.6449), (-73.9373, 40.8242)]
181
[(-73.7815, 40.6451), (-74.006, 40.7079)]
182
[(-73.7818, 40.6448), (-73.9766, 40.6872)]
183
[(-73.782, 40.6449), (-73.7416, 40.7148)]
184
[(-73.7821, 40.6449), (-74.1849, 40.6895)]
185
[(-73.781, 40.6478), (-73.7335, 40.6736)]
186
[(-73.782, 40.6449), (-73.9921, 40.7198)]
187
[(-73.7819, 40.6446), (-73.9258, 40.6541)]
188
[(-73.7806, 40.6461), (-73.9581, 40.5774)]
189
[(-73.7819, 40.6447), (-73.97800000000001, 40.7307)]
190
[(-73.7821, 40.6446), (-73.7981, 40.668)]
191
[(-73.7823, 40.6446), (-73.9332, 40.689)]
192
[(-73.7823, 40.6448), (-73.7758, 40.7831)]
193
[(-73.782, 40.6449), (-73.957, 40.7165)]
194
[(-73.7819, 40.6447), (-73.9705, 40.6862)]
195
[(-73.7819, 40.6447), (-73.9857, 40.7679)]
196
[(-73.781, 40.6451), (-73.9677, 40.6023)]
197
[(-73.7818, 40.6448), (-73.9835, 40.775999999999996)]
198
In [11]:
def plot_paths(paths,map_data):
    
    for path in paths:
        
        line = folium.PolyLine(
            path,
            weight=1,
            color='#0A8A9F'
        ).add_to(map_data)
    
    return map_data
In [12]:
map_data = generate_map([40.7128, -74.008],"cartodbpositron","pla","plo",'#0A8A9F',"maplat","maplong",'#f68e56')
In [13]:
plot_paths(routes, map_data)
Out[13]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [ ]: